<!DOCTYPE html>
<html lang="zh_Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="cpmFX379Hw.chiangyung.github.io">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chiangyung.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","Pisces | Gemini":240,"width":240,"display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本文给出了我总结的用于MPI三维数据传递的一些例子。">
<meta property="og:type" content="article">
<meta property="og:title" content="MPI三维数据传递">
<meta property="og:url" content="https://chiangyung.github.io/posts/22859/index.html">
<meta property="og:site_name" content="姜勇的博客">
<meta property="og:description" content="本文给出了我总结的用于MPI三维数据传递的一些例子。">
<meta property="article:published_time" content="2020-01-07T04:40:22.000Z">
<meta property="article:modified_time" content="2020-04-25T14:29:07.207Z">
<meta property="article:author" content="ChiangYung">
<meta property="article:tag" content="Programming">
<meta property="article:tag" content="MPI">
<meta property="article:tag" content="Parallel Computing">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://chiangyung.github.io/posts/22859/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh_Hans'
  };
</script>

  <title>MPI三维数据传递 | 姜勇的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="姜勇的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">姜勇的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">26</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">3</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">41</span></a>

  </li>
        <li class="menu-item menu-item-合集">

    <a href="/collections/" rel="section"><i class="fa fa-folder fa-fw"></i>合集</a>

  </li>
        <li class="menu-item menu-item-课程">

    <a href="/course/" rel="section"><i class="fa fa-graduation-cap fa-fw"></i>课程</a>

  </li>
        <li class="menu-item menu-item-读书">

    <a href="/books/" rel="section"><i class="fa fa-book fa-fw"></i>读书</a>

  </li>
        <li class="menu-item menu-item-旅行">

    <a href="/travels/" rel="section"><i class="fa fa-train fa-fw"></i>旅行</a>

  </li>
        <li class="menu-item menu-item-观影">

    <a href="/videos/" rel="section"><i class="fa fa-tv fa-fw"></i>观影</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh_Hans">
    <link itemprop="mainEntityOfPage" href="https://chiangyung.github.io/posts/22859/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/upload/E8.png">
      <meta itemprop="name" content="ChiangYung">
      <meta itemprop="description" content="我于蒙昧之中，探寻宇宙万物。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="姜勇的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MPI三维数据传递
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-01-07 12:40:22" itemprop="dateCreated datePublished" datetime="2020-01-07T12:40:22+08:00">2020-01-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-04-25 22:29:07" itemprop="dateModified" datetime="2020-04-25T22:29:07+08:00">2020-04-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Programming/" itemprop="url" rel="index"><span itemprop="name">Programming</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>
            <div class="post-description">本文给出了我总结的用于MPI三维数据传递的一些例子。</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>本文给出了MPI三维数据传递的一个实例。一维的MPI数据传递比较简单，直接使用<code>MPI_Sendrecv()</code>函数即可。但是三维MPI数据传递比较复杂，需要对进程在X/Y/Z三个方向进行划分，然后分别对不同方向上的数据进行传递。下面直接给出示例代码（默认总进程为8，X/Y/Z方向进程各为2）：</p>
<h2 id="mpi-helper-h"><a href="#mpi-helper-h" class="headerlink" title="mpi_helper.h"></a>mpi_helper.h</h2><p>头文件主要实现了三个方向的数据传递函数。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @file    mppic/commheaders/mpi_helper.h</span></span><br><span class="line"><span class="comment"> * @brief   MPI helper functions</span></span><br><span class="line"><span class="comment"> * @author  Jiang Yong [chiangyung@aliyun.com]</span></span><br><span class="line"><span class="comment"> * @version 2.0.p</span></span><br><span class="line"><span class="comment"> * @note    Updating for 3D passing functions.</span></span><br><span class="line"><span class="comment"> * @date    Thu 02 Aug 2018 09:17:29 PM CST</span></span><br><span class="line"><span class="comment"> ******************************************************************************/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> nx = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> ny = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> nz = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_XF_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="keyword">sizeof</span>(T) * length;</span><br><span class="line">    <span class="keyword">int</span> fid, tid, ix, iy, iz;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    iz = (rank / (nx * ny));</span><br><span class="line">    iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    fid = (ix &gt; <span class="number">0</span>) ? rank - <span class="number">1</span> : MPI_PROC_NULL;</span><br><span class="line">    tid = (ix &lt; nx - <span class="number">1</span>) ? rank + <span class="number">1</span> : MPI_PROC_NULL;</span><br><span class="line"></span><br><span class="line">    MPI_Sendrecv(src, l, MPI_BYTE, tid, <span class="number">0</span>,</span><br><span class="line">            dst, l, MPI_BYTE, fid, <span class="number">0</span>,</span><br><span class="line">            MPI_COMM_WORLD, &amp;status);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_XB_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="keyword">sizeof</span>(T) * length;</span><br><span class="line">    <span class="keyword">int</span> fid, tid, ix, iy, iz;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    iz = (rank / (nx * ny));</span><br><span class="line">    iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    tid = (ix &gt; <span class="number">0</span>) ? rank - <span class="number">1</span> : MPI_PROC_NULL;</span><br><span class="line">    fid = (ix &lt; nx - <span class="number">1</span>) ? rank + <span class="number">1</span> : MPI_PROC_NULL;</span><br><span class="line"></span><br><span class="line">    MPI_Sendrecv(src, l, MPI_BYTE, tid, <span class="number">0</span>,</span><br><span class="line">            dst, l, MPI_BYTE, fid, <span class="number">0</span>,</span><br><span class="line">            MPI_COMM_WORLD, &amp;status);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_YF_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="keyword">sizeof</span>(T) * length;</span><br><span class="line">    <span class="keyword">int</span> fid, tid, ix, iy, iz;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    iz = (rank / (nx * ny));</span><br><span class="line">    iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    fid = (iy &gt; <span class="number">0</span>) ? rank - nx : MPI_PROC_NULL;</span><br><span class="line">    tid = (iy &lt; ny - <span class="number">1</span>) ? rank + nx : MPI_PROC_NULL;</span><br><span class="line"></span><br><span class="line">    MPI_Sendrecv(src, l, MPI_BYTE, tid, <span class="number">0</span>,</span><br><span class="line">            dst, l, MPI_BYTE, fid, <span class="number">0</span>,</span><br><span class="line">            MPI_COMM_WORLD, &amp;status);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_YB_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="keyword">sizeof</span>(T) * length;</span><br><span class="line">    <span class="keyword">int</span> fid, tid, ix, iy, iz;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    iz = (rank / (nx * ny));</span><br><span class="line">    iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    tid = (iy &gt; <span class="number">0</span>) ? rank - nx : MPI_PROC_NULL;</span><br><span class="line">    fid = (iy &lt; ny - <span class="number">1</span>) ? rank + nx : MPI_PROC_NULL;</span><br><span class="line"></span><br><span class="line">    MPI_Sendrecv(src, l, MPI_BYTE, tid, <span class="number">0</span>,</span><br><span class="line">            dst, l, MPI_BYTE, fid, <span class="number">0</span>,</span><br><span class="line">            MPI_COMM_WORLD, &amp;status);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_ZF_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="keyword">sizeof</span>(T) * length;</span><br><span class="line">    <span class="keyword">int</span> fid, tid, ix, iy, iz;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    iz = (rank / (nx * ny));</span><br><span class="line">    iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    fid = (iz &gt; <span class="number">0</span>) ? rank - nx * ny : MPI_PROC_NULL;</span><br><span class="line">    tid = (iz &lt; nz - <span class="number">1</span>) ? rank + nx * ny : MPI_PROC_NULL;</span><br><span class="line"></span><br><span class="line">    MPI_Sendrecv(src, l, MPI_BYTE, tid, <span class="number">0</span>,</span><br><span class="line">            dst, l, MPI_BYTE, fid, <span class="number">0</span>,</span><br><span class="line">            MPI_COMM_WORLD, &amp;status);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_ZB_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> l = <span class="keyword">sizeof</span>(T) * length;</span><br><span class="line">    <span class="keyword">int</span> fid, tid, ix, iy, iz;</span><br><span class="line">    MPI_Status status;</span><br><span class="line"></span><br><span class="line">    iz = (rank / (nx * ny));</span><br><span class="line">    iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    fid = (iz &lt; nz - <span class="number">1</span>) ? rank + nx * ny : MPI_PROC_NULL;</span><br><span class="line">    tid = (iz &gt; <span class="number">0</span>) ? rank - nx * ny : MPI_PROC_NULL;</span><br><span class="line"></span><br><span class="line">    MPI_Sendrecv(src, l, MPI_BYTE, tid, <span class="number">0</span>,</span><br><span class="line">            dst, l, MPI_BYTE, fid, <span class="number">0</span>,</span><br><span class="line">            MPI_COMM_WORLD, &amp;status);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_X_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    MPI_XF_Sendrecv(rank, src, dst, length);</span><br><span class="line">    MPI_XB_Sendrecv(rank, src, dst, length);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_Y_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    MPI_YF_Sendrecv(rank, src, dst, length);</span><br><span class="line">    MPI_YB_Sendrecv(rank, src, dst, length);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_Z_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    MPI_ZF_Sendrecv(rank, src, dst, length);</span><br><span class="line">    MPI_ZB_Sendrecv(rank, src, dst, length);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">MPI_XYZ_Sendrecv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> rank, T *src, T *dst, <span class="keyword">int</span> length)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    MPI_X_Sendrecv(rank, src, dst, length);</span><br><span class="line">    MPI_Y_Sendrecv(rank, src, dst, length);</span><br><span class="line">    MPI_Z_Sendrecv(rank, src, dst, length);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="mpi-test-cpp"><a href="#mpi-test-cpp" class="headerlink" title="mpi_test.cpp"></a>mpi_test.cpp</h2><p>主函数主要用来测试数据传递是否正确。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @file    mpitest.cpp</span></span><br><span class="line"><span class="comment"> * @brief</span></span><br><span class="line"><span class="comment"> * @authro  Jiang Yong [chiangyung@aliyun.com]</span></span><br><span class="line"><span class="comment"> * @version</span></span><br><span class="line"><span class="comment"> * @date    Tue 09 Oct 2018 08:13:30 PM CST</span></span><br><span class="line"><span class="comment"> ******************************************************************************/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mpi_helper.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> rank, size;</span><br><span class="line"></span><br><span class="line">    MPI_Init(&amp;argc, &amp;argv);</span><br><span class="line">    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);</span><br><span class="line">    MPI_Comm_size(MPI_COMM_WORLD, &amp;size);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* The indices can be acquired by MPI_Cart_coords function */</span></span><br><span class="line">    <span class="keyword">int</span> iz = (rank / (nx * ny));</span><br><span class="line">    <span class="keyword">int</span> iy = (rank % (nx * ny)) / nx;</span><br><span class="line">    <span class="keyword">int</span> ix = (rank % (nx * ny)) % nx;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* int dims[3] = &#123;2, 2, 2&#125;; */</span></span><br><span class="line">    <span class="comment">/* int periods[3] = &#123;0, 0, 0&#125;; */</span></span><br><span class="line">    <span class="comment">/* int rank3d, coords[3]; */</span></span><br><span class="line">    <span class="comment">/* MPI_Comm MPI_COMM_3D; */</span></span><br><span class="line">    <span class="comment">/* MPI_Cart_create(MPI_COMM_WORLD, 3, dims, periods, 1, &amp;MPI_COMM_3D); */</span></span><br><span class="line">    <span class="comment">/* MPI_Comm_rank(MPI_COMM_3D, &amp;rank3d); */</span></span><br><span class="line">    <span class="comment">/* MPI_Cart_coords(MPI_COMM_3D, rank3d, 3, coords); */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* sleep(rank); */</span></span><br><span class="line">    <span class="comment">/* printf("%d (%d %d %d) (%d %d %d)\n", */</span></span><br><span class="line">    <span class="comment">/*         rank, iz, iy, ix, coords[0], coords[1], coords[2]); */</span></span><br><span class="line">    <span class="comment">/* MPI_Barrier(MPI_COMM_WORLD); */</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">double</span> a[N][N][N];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">                a[i][j][k] = rank;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> 0</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (ix != <span class="number">0</span>) &#123;</span><br><span class="line">                a[<span class="number">0</span>][j][k] /= <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (ix != nx - <span class="number">1</span>) &#123;</span><br><span class="line">                a[<span class="number">3</span>][j][k] /= <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (iy != <span class="number">0</span>) &#123;</span><br><span class="line">                a[i][<span class="number">0</span>][k] /= <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (iy != ny - <span class="number">1</span>) &#123;</span><br><span class="line">                a[i][<span class="number">3</span>][k] /= <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (iz != <span class="number">0</span>) &#123;</span><br><span class="line">                a[i * N + j][<span class="number">0</span>] /= <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (iz != nz - <span class="number">1</span>) &#123;</span><br><span class="line">                a[i * N + j][<span class="number">3</span>] /= <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> 0</span></span><br><span class="line">    <span class="keyword">if</span> (rank == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">                    <span class="built_in">printf</span>(<span class="string">"%lf "</span>, a[i][j][k]);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    MPI_Barrier(MPI_COMM_WORLD);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">double</span> xb[N * N], xbb[N * N];</span><br><span class="line">    <span class="keyword">double</span> xf[N * N], xff[N * N];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">            xb[j * N + k] = a[<span class="number">0</span>][j][k];</span><br><span class="line">            xbb[j * N + k] = <span class="number">0</span>;</span><br><span class="line">            xf[j * N + k] = a[N - <span class="number">1</span>][j][k];</span><br><span class="line">            xff[j * N + k] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    MPI_XF_Sendrecv&lt;<span class="keyword">double</span>&gt;(rank, xf, xbb, N * N);</span><br><span class="line">    MPI_XB_Sendrecv&lt;<span class="keyword">double</span>&gt;(rank, xb, xff, N * N);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">double</span> yb[N * N], ybb[N * N];</span><br><span class="line">    <span class="keyword">double</span> yf[N * N], yff[N * N];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">            yb[i * N + k] = a[i][<span class="number">0</span>][k];</span><br><span class="line">            ybb[i * N + k] = <span class="number">0</span>;</span><br><span class="line">            yf[i * N + k] = a[i][N - <span class="number">1</span>][k];</span><br><span class="line">            yff[i * N + k] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    MPI_YF_Sendrecv&lt;<span class="keyword">double</span>&gt;(rank, yf, ybb, N * N);</span><br><span class="line">    MPI_YB_Sendrecv&lt;<span class="keyword">double</span>&gt;(rank, yb, yff, N * N);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">double</span> zb[N * N], zbb[N * N];</span><br><span class="line">    <span class="keyword">double</span> zf[N * N], zff[N * N];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">            zb[i * N + j] = a[i][j][<span class="number">0</span>];</span><br><span class="line">            zbb[i * N + j] = <span class="number">0</span>;</span><br><span class="line">            zf[i * N + j] = a[i][j][N - <span class="number">1</span>];</span><br><span class="line">            zff[i * N + j] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    MPI_ZF_Sendrecv&lt;<span class="keyword">double</span>&gt;(rank, zf, zbb, N * N);</span><br><span class="line">    MPI_ZB_Sendrecv&lt;<span class="keyword">double</span>&gt;(rank, zb, zff, N * N);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; N; j++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">            a[<span class="number">0</span>][j][k] += xbb[j * N + k];</span><br><span class="line">            a[N - <span class="number">1</span>][j][k] += xff[j * N + k];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; N - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; N; k++) &#123;</span><br><span class="line">            a[i][<span class="number">0</span>][k] += ybb[i * N + k];</span><br><span class="line">            a[i][N - <span class="number">1</span>][k] += yff[i * N + k];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; N - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; N - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            a[i][j][<span class="number">0</span>] += zbb[i * N + j];</span><br><span class="line">            a[i][j][N - <span class="number">1</span>] += zff[i * N + j];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> 1</span></span><br><span class="line">    <span class="keyword">if</span> (rank == <span class="number">6</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = N - <span class="number">1</span>; k &gt;= <span class="number">0</span>; k--) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = N - <span class="number">1</span>; j &gt;= <span class="number">0</span>; j--) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++) &#123;</span><br><span class="line">                    <span class="built_in">printf</span>(<span class="string">"%lf "</span>, a[i][j][k]);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    MPI_Finalize();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
      
  <div class="popular-posts-header">相关文章</div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/35856/" rel="bookmark">LBM-MPI测试程序</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/50341/" rel="bookmark">MPI进程通讯头文件</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/59233/" rel="bookmark">MPI程序的GDB调试</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/55404/" rel="bookmark">MPI学习记录（二）</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/18454/" rel="bookmark">MPI学习记录（一）</a></div>
    </li>
  </ul>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>ChiangYung
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chiangyung.github.io/posts/22859/" title="MPI三维数据传递">https://chiangyung.github.io/posts/22859/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Programming/" rel="tag"><i class="fa fa-tag"></i> Programming</a>
              <a href="/tags/MPI/" rel="tag"><i class="fa fa-tag"></i> MPI</a>
              <a href="/tags/Parallel-Computing/" rel="tag"><i class="fa fa-tag"></i> Parallel Computing</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/55404/" rel="prev" title="MPI学习记录（二）">
      <i class="fa fa-chevron-left"></i> MPI学习记录（二）
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/59233/" rel="next" title="MPI程序的GDB调试">
      MPI程序的GDB调试 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#mpi-helper-h"><span class="nav-number">1.</span> <span class="nav-text">mpi_helper.h</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mpi-test-cpp"><span class="nav-number">2.</span> <span class="nav-text">mpi_test.cpp</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="ChiangYung"
      src="/upload/E8.png">
  <p class="site-author-name" itemprop="name">ChiangYung</p>
  <div class="site-description" itemprop="description">我于蒙昧之中，探寻宇宙万物。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chiangyung" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chiangyung" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:chiangyoung@outlook.com" title="E-Mail → mailto:chiangyoung@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://openfoamwiki.net/index.php/Main_Page" title="http:&#x2F;&#x2F;openfoamwiki.net&#x2F;index.php&#x2F;Main_Page" rel="noopener" target="_blank">OpenFOAMWiki</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ChiangYung</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">207k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:08</span>
</div>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
    <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>

<script src="/js/kity.min.js"></script>

<script src="/js/kityminder.core.min.js"></script>

<script src="/js/mindmap.js"></script>

<script src="/js/mindmap.min.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  

</body>
</html>
